package com.zsr.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author zsr
 * @Desc
 * @Date 2021/5/15 21:38
 */
@RestController
public class JdbcController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库的所有信息
    //没有实体类，数据库中的东西，怎么获取？  Map
    @GetMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql="select * from users";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @GetMapping("/addUser")
    public String addUser(){
        String sql ="insert into javaweb_task_demo.users values(null,'xiao','123456','1301888091@qq.com','2021/5/15')";
        jdbcTemplate.update(sql);
        return "add-ok";
    }

    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id")int id){
        String sql ="update javaweb_task_demo.users set username= ?,password=?,email=?,birthday=?   where id=?";
        //封装
        Object[] objects=new Object[5];
        objects[0]="sa";
        objects[1]="12346";
        objects[2]="131581@qq.com";
        objects[3]=new Date();
        objects[4]=id;
        jdbcTemplate.update(sql,objects);
        return "update-ok";
    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id")int id){
        String sql ="delete from users where id=?";
        jdbcTemplate.update(sql,id);
        return "delete-ok";
    }
}
